package com.xzz.finger;

import java.util.Stack;

/**
 * @author: hhz
 * @create: 2022-02-21 17:50
 * 剑指 Offer 30. 包含min函数的栈
 **/
public class MinStack {
    Stack<Integer> stack = new Stack<>();
    Stack<Integer> help = new Stack<>();

    /**
     * initialize your data structure here.
     */
    public MinStack() {

    }

    public void push(int x) {
        stack.push(x);
        if (help.isEmpty() || help.peek() >= x) {
            help.push(x);
        } else {
            help.push(help.peek());
        }

    }

    public void pop() {
        if (!stack.isEmpty()) {
            stack.pop();
            help.pop();
        }

    }

    public int top() {
        return stack.peek();
    }

    public int min() {
        return help.peek();
    }

    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);
        System.out.println(minStack.min());
        minStack.pop();
        System.out.println(minStack.top());
        System.out.println(minStack.min());

    }
}
